let {
	operate = "2", //查询的数据  1: 余额变更 2: 收益变更 
		offset = 2, //分页
		limit = 5, //每页多少条
		user_user = 38,
		start_year_month = 1698768000000,
		end_year_month = 1701360000000
} = payload;
let {
	id
} = tokenToData(); //获取用户id
user_user = user_user || id;
switch (operate) {
	case "1":
		return queryBalance();
	case "2":
		return queryIncome();
	default:
		setReturn(payload, "fail:失败", "未知操作类型，请检查!")
}

function queryBalance() {
	let list = query({
		model: "user_assets",
		where: {
			user_user: {
				_eq: user_user
			},
		},
		field_string: `id type balance describe status
		user_assets_detail_aggregate(where:{blean:{_eq:false},created_at:{_gt:${start_year_month},_lt:${end_year_month}}}){aggregate{count}}
		user_assets_detail(order_by: {created_at: desc},where:{blean:{_eq:false},created_at:{_gt:${start_year_month},_lt:${end_year_month}}} offset:${(offset-1)*limit} limit:${limit}){id blean init_val created_at change_val balance describe attach_data}`
	})
	return {
		data: list?.reduce((arr, item) => {
			let {
				user_assets_detail_aggregate,
				user_assets_detail = [], ...info
			} = item
			arr = {
				...info,
				pages: user_assets_detail_aggregate?.aggregate?.count ?? 0,
				list: user_assets_detail
			}
			return arr
		}, []),
	}
}

function queryIncome() {
	// let list = query({
	// 	model: "user_assets1",
	// 	where: {
	// 		user_user: {
	// 			_eq: user_user
	// 		},
	// 	},
	// 	field_string: `id type status
	// 	user_assets_detail_aggregate(where:{type:{_ilike:"%获得%"},blean:{_eq:true},created_at:{_gt:${start_year_month},_lt:${end_year_month}}}){aggregate{count sum{change_val}}}
	// 	user_assets_detail(order_by: {created_at: desc } where:{type:{_ilike:"%获得%"},blean:{_eq:true},created_at:{_gt:${start_year_month},_lt:${end_year_month}}} offset:${(offset-1)*limit} limit:${limit}){ id blean
	// 	init_val created_at change_val balance describe attach_data }`
	// })
	// return {
	// 	data: list?.reduce((arr, item) => {
	// 		let {
	// 			user_assets_detail_aggregate,
	// 			user_assets_detail = [],
	// 			...info
	// 		} = item
	// 		arr = {
	// 			...info,
	// 			balance: user_assets_detail_aggregate?.aggregate?.sum?.change_val ?? 0,
	// 			pages: user_assets_detail_aggregate?.aggregate?.count ?? 0,
	// 			list: user_assets_detail
	// 		}
	// 		return arr
	// 	}, []),
	// }
	let [user_assets] = query({
		model: "user_assets",
		where: {
			user_user: {
				_eq: user_user
			},
		},
		field_string: `id type status`
	})
	let where = {
		user_assets_user_assets: {
			_eq: user_assets?.id
		},
		type: {
			_ilike: "%获得%"
		},
		blean: {
			_eq: true
		},
		_and: [{
			created_at: {
				_gt: start_year_month,
			}
		}, {
			created_at: {
				_lt: end_year_month
			}
		}]
	}
	let {
		user_assets_detail_aggregate,
		user_assets_detail
	} = batch_query([{
		model: "user_assets_detail_aggregate",
		where: where,
		field_string: "aggregate{count sum{change_val}}"
	}, {
		model: "user_assets_detail",
		where: where,
		offset: (offset - 1) * limit,
		limit: limit,
		field_string: "id blean init_val created_at change_val balance describe attach_data "
	}]) || {}
	return {
		data: [{
			...user_assets,
			balance: user_assets_detail_aggregate?.aggregate?.sum?.change_val ?? 0,
			pages: user_assets_detail_aggregate?.aggregate?.count ?? 0,
			list: user_assets_detail
		}]
	}
}